package com.hongxun.design.jungong.adapter;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hongxun.design.R;
import com.hongxun.design.jungong.model.SspPhoto;
import com.hongxun.design.listeners.MClickListener;
import com.hongxun.design.utils.TimeUtil;
import com.hongxun.design.utils.imageutil.UriUtil;

import java.util.Date;
import java.util.List;

/**
 * Created by cqcqwucp on 2017/11/13.
 */

public class FragmentPhotoAdapter extends myBaseAdapter<SspPhoto> {
    private Handler handler;
    private int inx1, inx2;

    public FragmentPhotoAdapter(Activity activity, List<SspPhoto> sspPhotos) {
        super(activity, sspPhotos);
    }

    public void setHandlerAndInx(Object... obj) {
        this.handler = (Handler) obj[0];
        this.inx1 = (int) obj[1];
        this.inx2 = (int) obj[2];
    }

    @Override
    public View getV(int position, View convertView, SspPhoto obj) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.fragmentphoto_lvitem1, null);
            holder = new ViewHolder(convertView);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.setData(obj);
        return convertView;
    }

    class ViewHolder {
        ImageView lvitem1Iv1;
        TextView lvitem1Tv1;
        TextView lvitem1Tv2;
        TextView lvitem1Tv4;

        ViewHolder(View view) {
            view.setTag(this);
            findByIDs(view);
        }

        private void findByIDs(View view) {
            lvitem1Iv1 = (ImageView) view.findViewById(R.id.lvitem1_iv1);
            lvitem1Tv1 = (TextView) view.findViewById(R.id.lvitem1_tv1);
            lvitem1Tv2 = (TextView) view.findViewById(R.id.lvitem1_tv2);
            lvitem1Tv4 = (TextView) view.findViewById(R.id.lvitem1_tv4);
        }

        public void setData(SspPhoto obj) {
            lvitem1Iv1.setImageURI(UriUtil.getUri(obj.getImgaddress()));
            lvitem1Tv1.setText(obj.getPlistone());
            String time = obj.getFoundertime();
            if (null != time && TextUtils.isDigitsOnly(time))
                time = TimeUtil.timeSFM(new Date(Long.valueOf(time)));
            lvitem1Tv2.setText(time);
            lvitem1Tv4.setText(obj.getRemarks());

//            final Handler handler1 = new Handler() {
//                @Override
//                public void handleMessage(Message msg) {
//                    tList.remove(msg.obj);
//                    notifyDataSetChanged();
//                    ToastShow.showShort(activity, "删除成功");
//
//                    Message message = handler.obtainMessage();
//                    message.obj = msg.obj;
//                    message.what = inx1;
//                    handler.sendMessage(message);
//                }
//            };
            //删除11
//            lvitem1Tv3.setOnClickListener(new MClickListener(tList.indexOf(obj), obj) {
//                @Override
//                public void onClick(View view, int position, Object msg) {
//                    List<String> list = new ArrayList<>();
//                    list.add("删除提示");
//                    list.add("是否删除照片");
//                    list.add("否");
//                    list.add("是");
//                    MyDialog.recordDel(activity, list, handler1, msg, 0, 0);
//                }
//            });
            //查看大图12
            lvitem1Iv1.setOnClickListener(new MClickListener(tList.indexOf(obj), obj) {
                @Override
                public void onClick(View view, int position, Object msg) {
                    Message message = handler.obtainMessage();
                    message.obj = ((SspPhoto) msg).getImgaddress();
                    message.what = inx2;
                    handler.sendMessage(message);
                }
            });
        }
    }
}
